//
//  BaseViewController.m
//  NewNym
//
//  Created by Gideon on 16/2/15.
//  Copyright © 2016年 LiZhenning. All rights reserved.
//

#import "BaseViewController.h"


@interface BaseViewController ()
<
TZImagePickerControllerDelegate,
UIImagePickerControllerDelegate,
UINavigationControllerDelegate
>
{
    UIView *bgView;
    UIImageView *navBarHairlineImageView;
    
    
    UIButton *disBtn;
    UIView *backMyView;
    UIImageView *disImageView;
    
}
@end

@implementation BaseViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.view setBackgroundColor:HexRGBA(0xf0f1f2, 1)];
    
    
    navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = YES;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    [self adaptiveIOS11];
    
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    {
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }
    
    
}

#pragma mark -- 适配IOS11
- (void)adaptiveIOS11{
    
    if (@available(iOS 11.0, *)) {
        
        [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [UITableView appearance].estimatedRowHeight = 0;
        [UITableView appearance].estimatedSectionFooterHeight = 0;
        [UITableView appearance].estimatedSectionHeaderHeight = 0;
    }
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}


-(BOOL)shouldAutorotate{
    
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    
    return UIInterfaceOrientationMaskPortrait;
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

-(void)setNavLine:(UIColor*)color{
    
    
    for (UIView *itemLine in self.navigationController.navigationBar.subviews) {
        
        if (itemLine.size.height == 0.5) {
            
            [itemLine removeFromSuperview];
        }
        
    }
    
    UIView * line=[[UIView alloc]initWithFrame:CGRectMake(0, 43.5, kScreenWidth, 0.5)];
    [line setBackgroundColor:color];
    [self.navigationController.navigationBar addSubview:line];
    
}

-(void)setDeleteNavLine{
    
    for (UIView *itemLine in self.navigationController.navigationBar.subviews) {
        
        if (itemLine.size.height == 0.5) {
            
            [itemLine removeFromSuperview];
        }
        
    }
}

- (void)setBackLeftBarButtonItemAction:(SEL)action
{
    
    UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
    firstButton.frame = CGRectMake(0, 0, 44, 44);
    [firstButton setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [firstButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    firstButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [firstButton setImageEdgeInsets:UIEdgeInsetsMake(0, -15 * kAutoScanleX, 0, 0)];
    
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:firstButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    
}


-(void)setBackRightBarButtonItemAction:(SEL)action text:(NSString *)text{
    
    CGFloat width = [self widthByStringContent:text andFontSize:16 * kAutoScanleX]+20;
    
    UIButton *rightbBarButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, width, 44)];
    [rightbBarButton setTitle:text forState:(UIControlStateNormal)];
    [rightbBarButton setTitleColor:HexRGBA(0xffffff, 1) forState:(UIControlStateNormal)];
    rightbBarButton.titleLabel.font = [UIFont systemFontOfSize:16 * kAutoScanleX];
    [rightbBarButton addTarget:self action:action forControlEvents:(UIControlEventTouchUpInside)];
    rightbBarButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [rightbBarButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightbBarButton];
    
}



- (void)setBackLeftBarButtonItemAction:(SEL)action image:(UIImage *)image{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    view.backgroundColor = [UIColor clearColor];
    UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
    firstButton.frame = CGRectMake(0, 0, 44, 44);
    [firstButton setImage:image forState:UIControlStateNormal];
    [firstButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    firstButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [firstButton setImageEdgeInsets:UIEdgeInsetsMake(0, 5 * kScreenWidth / 375.0, 0, 0)];
    
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:firstButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
}



- (void)setBackRightBarButtonItemAction:(SEL)action image:(UIImage *)image{
    
    UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
    firstButton.frame = CGRectMake(0, 0, 44, 44);
    [firstButton setImage:image forState:UIControlStateNormal];
    [firstButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    firstButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:firstButton];
    
}

-(void)setBackRightArrow:(UIView *)view{
    
    UIImageView *imageView=[[UIImageView alloc]init];
    [imageView setImage:[UIImage imageNamed:@"icon_arrow"]];
    [view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view);
        make.right.equalTo(view).offset(-15 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(9 * kAutoScanleX, 15 * kAutoScanleX));
    }];
    
}

-(void)setBackRightDarkArrow:(UIView *)view{
    
    UIImageView *imageView=[[UIImageView alloc]init];
    [imageView setImage:[UIImage imageNamed:@"icon_arrow"]];
    [view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view);
        make.right.equalTo(view).offset(-15 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(9 * kAutoScanleX, 15 * kAutoScanleX));
    }];
    
}

-(void)setTopLine:(UIView *)view color:(UIColor *)color{
    
    UIView *line=[[UIView alloc]init];
    [line setBackgroundColor:color];
    [view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(view);
        make.height.mas_equalTo(0.5);
    }];
    
}

-(void)setBomLine:(UIView *)view color:(UIColor *)color{
    
    
    UIView *line=[[UIView alloc]init];
    [line setBackgroundColor:color];
    [view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(view);
        make.height.mas_equalTo(0.5);
    }];
    
    
}

-(void)setHline:(UIView *)view color:(UIColor *)color height:(CGFloat)height x:(CGFloat)x{
    
    
    UIView *line=[[UIView alloc]init];
    [line setBackgroundColor:color];
    [view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view).offset(x);
        make.centerY.equalTo(view);
        make.height.mas_equalTo(height);
        make.width.mas_equalTo(0.5);
    }];
    
    
}


-(UIImage *)ipMaskedImage:(UIImage *)image color:(UIColor *)color
{
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
    CGContextRef c = UIGraphicsGetCurrentContext();
    [image drawInRect:rect];
    CGContextSetFillColorWithColor(c, [color CGColor]);
    CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
    CGContextFillRect(c, rect);
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}

- (void)setShadowInitWithView:(UIView *)view color:(UIColor *)color aplan:(CGFloat)aplan{
    
    
    
    
}

#pragma mark--照相选择
-(void)upLoadPhoto
{
    
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    imagePickerVc.allowTakeVideo = NO;
    imagePickerVc.showSelectBtn = NO;
    imagePickerVc.allowCrop = YES;
    imagePickerVc.needCircleCrop = YES;
    imagePickerVc.allowPickingVideo = NO;
    // 设置竖屏下的裁剪尺寸
    NSInteger left = 30 * kAutoScanleX;
    NSInteger widthHeight = kScreenWidth- 2 * left;
    NSInteger top = (kScreenHeight - widthHeight) / 2;
    imagePickerVc.cropRect = CGRectMake(left, top, widthHeight, widthHeight);
    [self presentViewController:imagePickerVc animated:YES completion:nil];
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
        if (self.finishPhoto) {
            self.finishPhoto(photos[0]);
        }
    }];
    
}
- (UIImage *)setImageSnapshotImageView:(UIView *)imageView
{
    
    CGRect rect = [imageView bounds];
    UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [imageView.layer renderInContext:context];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

- (void)setNavHidden:(BOOL)hidden{
    
    [self.navigationController.navigationBar setHidden:hidden];
}



//获取当地时间
- (NSString *)getCurrentTime {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

//1、判断输入的字符串是否全是中文
-(BOOL)IsChinese:(NSString *)str
{
    NSInteger count = str.length;
    NSInteger result = 0;
    for(int i=0; i< [str length];i++)
    {
        int a = [str characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff)//判断输入的是否是中文
        {
            result++;
        }
    }
    if (count == result) {//当字符长度和中文字符长度相等的时候
        return YES;
    }
    return NO;
}

//1、判断输入的字符串是否有中文
-(BOOL)IsHaveChinese:(NSString *)str
{
    for(int i=0; i< [str length];i++)
    {
        int a = [str characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff)//判断输入的是否是中文
        {
            return YES;
        }
    }
    return NO;
}

-(void)setShareSDKImage:(id)image
                   text:(NSString *)text
                  title:(NSString *)title
                    url:(NSURL *)url{
    
    
    NSArray * imageArray = @[image];
    
    if (imageArray) {
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:text
                                         images:imageArray
                                            url:url
                                          title:title
                                           type:SSDKContentTypeAuto];
        
        
        [ShareSDK showShareActionSheet:nil customItems:nil shareParams:shareParams sheetConfiguration:nil onStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
            switch (state) {
                case SSDKResponseStateSuccess:
                {
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                        message:nil
                                                                       delegate:nil
                                                              cancelButtonTitle:@"确定"
                                                              otherButtonTitles:nil];
                    [alertView show];
                    break;
                }
                case SSDKResponseStateFail:
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                    message:[NSString stringWithFormat:@"%@",error]
                                                                   delegate:nil
                                                          cancelButtonTitle:@"OK"
                                                          otherButtonTitles:nil, nil];
                    [alert show];
                    break;
                }
                default:
                    break;
            }
        }];
        
        
        //        [SSUIShareActionSheetStyle setItemNameFont:[UIFont systemFontOfSize:12 * kAutoScanleX]];
        //
        //        [SSUIShareActionSheetStyle setShareActionSheetStyle:ShareActionSheetStyleSimple];
        //
        //        [ShareSDK showShareActionSheet:nil
        //                                 items:nil
        //                           shareParams:shareParams
        //                   onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
        //
        //                       switch (state) {
        //                           case SSDKResponseStateSuccess:
        //                           {
        //                               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
        //                                                                                   message:nil
        //                                                                                  delegate:nil
        //                                                                         cancelButtonTitle:@"确定"
        //                                                                         otherButtonTitles:nil];
        //                               [alertView show];
        //                               break;
        //                           }
        //                           case SSDKResponseStateFail:
        //                           {
        //                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
        //                                                                               message:[NSString stringWithFormat:@"%@",error]
        //                                                                              delegate:nil
        //                                                                     cancelButtonTitle:@"OK"
        //                                                                     otherButtonTitles:nil, nil];
        //                               [alert show];
        //                               break;
        //                           }
        //                           default:
        //                               break;
        //                       }
        //                   }];
        //
    }
    
}


@end
